//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Services.FileSystem;
namespace DotNetNuke.UI.UserControls
{
	public abstract class URLTrackingControl : Framework.UserControlBase
	{
		protected System.Web.UI.WebControls.Label lblURL;
		protected System.Web.UI.WebControls.Label lblCreatedDate;
		protected System.Web.UI.WebControls.Panel pnlTrack;
		protected System.Web.UI.WebControls.Label lblTrackingURL;
		protected System.Web.UI.WebControls.Label lblClicks;
		protected System.Web.UI.WebControls.Label lblLastClick;
		protected System.Web.UI.WebControls.Panel pnlLog;
		protected System.Web.UI.WebControls.TextBox txtStartDate;
		protected System.Web.UI.WebControls.TextBox txtEndDate;
		protected System.Web.UI.WebControls.HyperLink cmdStartCalendar;
		protected System.Web.UI.WebControls.HyperLink cmdEndCalendar;
		protected System.Web.UI.WebControls.CompareValidator valStartDate;
		protected System.Web.UI.WebControls.CompareValidator valEndDate;
		protected System.Web.UI.WebControls.LinkButton cmdDisplay;
		protected System.Web.UI.WebControls.DataGrid grdLog;
		protected System.Web.UI.WebControls.Label lblLogURL;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.Label Label5;
		protected System.Web.UI.WebControls.Label Label6;
		protected System.Web.UI.WebControls.Label Label7;
		private string _URL = "";
		private string _FormattedURL = "";
		private string _TrackingURL = "";
		private int _ModuleID = -2;
		private string _localResourceFile;
		public string FormattedURL
		{
			get { return _FormattedURL; }
			set { _FormattedURL = value; }
		}
		public string TrackingURL
		{
			get { return _TrackingURL; }
			set { _TrackingURL = value; }
		}
		public string URL
		{
			get { return _URL; }
			set { _URL = value; }
		}
		public int ModuleID
		{
			get
			{
				int moduleID = _ModuleID;
				if (moduleID == -2)
				{
					if (Request.QueryString["mid"] != null)
					{
						Int32.TryParse(Request.QueryString["mid"], out moduleID);
					}
				}
				return moduleID;
			}
			set { _ModuleID = value; }
		}
		public string LocalResourceFile
		{
			get
			{
				string fileRoot;
				if (String.IsNullOrEmpty(_localResourceFile))
				{
					fileRoot = this.TemplateSourceDirectory + "/" + Services.Localization.Localization.LocalResourceDirectory + "/URLTrackingControl.ascx";
				}
				else
				{
					fileRoot = _localResourceFile;
				}
				return fileRoot;
			}
			set { _localResourceFile = value; }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdDisplay.Click += new EventHandler(cmdDisplay_Click);

			try
			{
				cmdStartCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtStartDate);
				cmdEndCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtEndDate);
				if (!Page.IsPostBack)
				{
					if (!String.IsNullOrEmpty(_URL))
					{
						lblLogURL.Text = URL;
						TabType URLType = Common.Globals.GetURLType(_URL);
						if (URLType == TabType.File && _URL.ToLower().StartsWith("fileid=") == false)
						{
							FileController objFiles = new FileController();
							lblLogURL.Text = "FileID=" + objFiles.ConvertFilePathToFileId(_URL, PortalSettings.PortalId).ToString();
						}
						UrlController objUrls = new UrlController();
						UrlTrackingInfo objUrlTracking = objUrls.GetUrlTracking(PortalSettings.PortalId, lblLogURL.Text, ModuleID);
						if (objUrlTracking != null)
						{
							if (String.IsNullOrEmpty(_FormattedURL))
							{
								lblURL.Text = Globals.LinkClick(URL, PortalSettings.ActiveTab.TabID, ModuleID, false);
								if (!lblURL.Text.StartsWith("http") && !lblURL.Text.StartsWith("mailto"))
								{
									lblURL.Text = Globals.AddHTTP(Request.Url.Host) + lblURL.Text;
								}
							}
							else
							{
								lblURL.Text = _FormattedURL;
							}
							lblCreatedDate.Text = objUrlTracking.CreatedDate.ToString();
							if (objUrlTracking.TrackClicks)
							{
								pnlTrack.Visible = true;
								if (String.IsNullOrEmpty(_TrackingURL))
								{
									if (!URL.StartsWith("http"))
									{
										lblTrackingURL.Text = Common.Globals.AddHTTP(Request.Url.Host);
									}
									lblTrackingURL.Text += Common.Globals.LinkClick(URL, PortalSettings.ActiveTab.TabID, ModuleID, objUrlTracking.TrackClicks);
								}
								else
								{
									lblTrackingURL.Text = _TrackingURL;
								}
								lblClicks.Text = objUrlTracking.Clicks.ToString();
								if (!Null.IsNull(objUrlTracking.LastClick))
								{
									lblLastClick.Text = objUrlTracking.LastClick.ToString();
								}
							}
							if (objUrlTracking.LogActivity)
							{
								pnlLog.Visible = true;
								txtStartDate.Text = DateTime.Today.AddDays(-6).ToShortDateString();
								txtEndDate.Text = DateTime.Today.AddDays(1).ToShortDateString();
							}
						}
					}
					else
					{
						this.Visible = false;
					}
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}

		}
		private void cmdDisplay_Click(object sender, System.EventArgs e)
		{
			try
			{
				string strStartDate = txtStartDate.Text;
				if (!String.IsNullOrEmpty(strStartDate))
				{
					strStartDate = strStartDate + " 00:00";
				}
				string strEndDate = txtEndDate.Text;
				if (!String.IsNullOrEmpty(strEndDate))
				{
					strEndDate = strEndDate + " 23:59";
				}
				UrlController objUrls = new UrlController();
				Services.Localization.Localization.LocalizeDataGrid(ref grdLog, this.LocalResourceFile);
				grdLog.DataSource = objUrls.GetUrlLog(PortalSettings.PortalId, lblLogURL.Text, ModuleID, Convert.ToDateTime(strStartDate), Convert.ToDateTime(strEndDate));
				grdLog.DataBind();
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
